﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;


namespace 盎宇宙服务
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 判断指定端口号是否被占用
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static Boolean IsPortOccupedFun1(Int32 port)
        {
            Boolean result = false;
            try
            {
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo("netstat", "-an");
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();
                String output = p.StandardOutput.ReadToEnd().ToLower();
                string ip1 = "127.0.0.1";
                string ip2 = "0.0.0.0";
                System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
                List<string> ipList = new List<string>();
                ipList.Add(ip1);
                ipList.Add(ip2);
                for (int i = 0; i < addressList.Length; i++)
                {
                    ipList.Add(addressList[i].ToString());
                }


                for (int i = 0; i < ipList.Count; i++)
                {
                    if (output.IndexOf("udp    " + ipList[i] + ":" + port.ToString()) >= 0)//"tcp " + 
                    {
                        result = true;
                        break;
                    }
                    //if (output.IndexOf("udp  " + ipList[i] + ":" + port.ToString()) >= 0)//
                    //{
                    //    result = true;
                    //    break;
                    //}
                }
            }
            catch (Exception ex)
            {
                //  Application.ActiveApplication.Output.Output(ex);
            }
            return result;
        }
    }
}
